#!/bin/bash

# createAll.sh
# created 2019 July 29

# This file automatically generates all of the figures and tables in  
# "Education and Attitudes toward Redistribution in the United States."

  

mainBatch=TRUE
appendixBatch=TRUE


# CREATE DIRECTORIES FOR OUTPUT FILES
if [ -d "batchOutput" ]
then
  echo "The batchOutput directory already exists."
else 
  `mkdir batchOutput`
fi


if [ -d "float_output" ]
then
  echo "The float_output directory already exists."
else 
  `mkdir float_output`
fi



# INSTALL "BULLOCK" PACKAGE INTO LOCAL PACKAGE LIBRARY
echo "Installing the 'Bullock' package into a local package library."
R CMD BATCH "--no-save" "installBullockPackage.R" "batchOutput/out_installBullockPackage.txt"


# DOWNLOAD AND PROCESS ANES AND GSS DATASETS
echo "Downloading and processing the ANES and GSS datasets. This will take a while."
R CMD BATCH "--no-save" "prepareDatasets.R" "batchOutput/out_PrepareDatasets.txt"


if [ "$mainBatch" = "TRUE" ]
then 

  echo "Making Figures 1-3 and Table 1."
  R CMD BATCH "--no-save" "float_code/Figure_1.R" "batchOutput/out_Figure_1.txt"

  # ARGUMENTS FOR FIGURE_2.R: STANDARDIZE  
  R CMD BATCH "--no-save --args FALSE" "float_code/Figure_2.R" "batchOutput/out_Figure_2.txt"

  R CMD BATCH "--no-save" "float_code/Table_1.R" "batchOutput/out_Table_1.txt"  

  # ARGUMENTS FOR FIGURE_3.R: ONE_COLUMN_ESTIMATOR, SHORT_PANEL
  R CMD BATCH "--no-save --args OLS FALSE" "float_code/Figure_3.R" "batchOutput/out_Figure_3.txt"            
fi



if [ "$appendixBatch" = "TRUE" ]
then
  
  echo "Making the tables and figures that appear in the online appendix."
  # ************************************************************************
  # * BACKGROUND INFORMATION
  # ************************************************************************  
  R CMD BATCH "--no-save" "float_code/Table_A01-A06.R" "batchOutput/out_Table_A01-A06.txt"
  R CMD BATCH "--no-save" "float_code/Table_A07.R"     "batchOutput/out_Table_A07.txt"
  R CMD BATCH "--no-save" "float_code/Figure_A01.R"    "batchOutput/out_Figure_A01.txt"  
  R CMD BATCH "--no-save" "float_code/Figure_A02.R"    "batchOutput/out_Figure_A02.txt"  
  R CMD BATCH "--no-save" "float_code/Figure_A03.R"    "batchOutput/out_Figure_A03.txt"  
    


  # ************************************************************************
  # * SUPPLEMENTS TO THE MAIN ANALYSES
  # ************************************************************************
  R CMD BATCH "--no-save" "float_code/Table_A08.R"       "batchOutput/out_Table_A08.txt"
  R CMD BATCH "--no-save" "float_code/Table_A09a-A14a.R" "batchOutput/out_Table_A09a-A14a.txt"
  R CMD BATCH "--no-save" "float_code/Table_A09b-A14b.R" "batchOutput/out_Table_A09b-A14b.txt"
  
  # ARGUMENTS FOR FIGURE_A04.R: TSLS_ESTIMATES_ON_BOTTOM, OTHER_PANELS, STANDARDIZE, WHITES_ONLY 
  R CMD BATCH "--no-save --args TRUE OLS FALSE FALSE" "float_code/Figure_A04.R"    "batchOutput/out_Figure_A04.txt"  

  # ARGUMENTS FOR FIGURE_2.R: STANDARDIZE  
  R CMD BATCH "--no-save --args TRUE" "float_code/Figure_2.R" "batchOutput/out_Figure_A05.txt"
  
  R CMD BATCH "--no-save" "float_code/Table_A15.R"  "batchOutput/out_Table_A15.txt"
  R CMD BATCH "--no-save" "float_code/Figure_A06.R" "batchOutput/out_Figure_A06.txt"  

  # ARGUMENTS FOR FIGURE_A04.R: TSLS_ESTIMATES_ON_BOTTOM, OTHER_PANELS, STANDARDIZE, WHITES_ONLY 
  R CMD BATCH "--no-save --args TRUE 2SLS_OVER_34 FALSE FALSE" "float_code/Figure_A04.R" "batchOutput/out_Figure_A07.txt"  

  R CMD BATCH "--no-save" "float_code/Figure_A08.R" "batchOutput/out_Figure_A08.txt"
  R CMD BATCH "--no-save" "float_code/Figure_A09.R" "batchOutput/out_Figure_A09.txt"

  # ARGUMENTS FOR FIGURE_A04.R: TSLS_ESTIMATES_ON_BOTTOM, OTHER_PANELS, STANDARDIZE, WHITES_ONLY 
  R CMD BATCH "--no-save --args TRUE SOUTH FALSE FALSE" "float_code/Figure_A04.R" "batchOutput/out_Figure_A10.txt"  

  # ARGUMENT FOR FIGURE_A11.R: EDUC_DIFF 
  R CMD BATCH "--no-save --args FALSE" "float_code/Figure_A11.R" "batchOutput/out_Figure_A11.txt"  
  R CMD BATCH "--no-save --args TRUE"  "float_code/Figure_A11.R" "batchOutput/out_Figure_A12.txt"  

  # ARGUMENTS FOR FIGURE_A04.R: TSLS_ESTIMATES_ON_BOTTOM, OTHER_PANELS, STANDARDIZE, WHITES_ONLY 
  R CMD BATCH "--no-save --args TRUE IMPUTED FALSE FALSE" "float_code/Figure_A04.R" "batchOutput/out_Figure_A13.txt"  

  R CMD BATCH "--no-save" "float_code/Figure_A14.R" "batchOutput/out_Figure_A14.txt"



  # ************************************************************************
  # * OTHER CONSIDERATIONS
  # ************************************************************************
  R CMD BATCH "--no-save" "float_code/Figure_A15.R" "batchOutput/out_Figure_A15.txt"
  R CMD BATCH "--no-save" "float_code/Figure_A16.R" "batchOutput/out_Figure_A16.txt"
  R CMD BATCH "--no-save" "float_code/Table_A17.R"  "batchOutput/out_Table_A17.txt"

  # ARGUMENTS FOR FIGURE_A04.R: TSLS_ESTIMATES_ON_BOTTOM, OTHER_PANELS, STANDARDIZE, WHITES_ONLY 
  R CMD BATCH "--no-save --args TRUE WHITES_ONLY FALSE FALSE" "float_code/Figure_A04.R" "batchOutput/out_Figure_A17.txt"  
  R CMD BATCH "--no-save --args TRUE HSGRAD      FALSE FALSE" "float_code/Figure_A04.R" "batchOutput/out_Figure_A18.txt"  
fi